#include "../Header/Globals.h"
#include "Game.h"

Game::Game()
: m_bIsPaused(false), m_bIsAlived(true)
{}

Game::~Game()
{
	//Release Game Instance
	SceneManager* sceneMgr = SceneManager::GetInstance();
	SAFE_DEL(sceneMgr);
	Device* device = Device::GetInstance();
	SAFE_DEL(device);
	FPSController* fpsCobtroller = FPSController::GetInstance();
	SAFE_DEL(fpsCobtroller);
	ViewController<ESWin32View>* viewCtrl = ViewController<ESWin32View>::GetInstance();
	SAFE_DEL(viewCtrl);
	ResourceManagement* resourceMgr = ResourceManagement::GetInstance();
	SAFE_DEL(resourceMgr);
}

__VOID Game::Run()
{
	ResourceManagement::GetInstance()->Init("");
	ResourceManagement::GetInstance()->Load();
	if (this->Init() != 0)
		return;

	if (!ViewController<ESWin32View>::GetInstance())
	{
		//Create Client View
		ViewController<ESWin32View>::GetInstance()->CreateView(Globals::m_iScreenWidth, Globals::m_iScreenHeight, TEXT("Game Engine"), FALSE, ES_WINDOW_RGB | ES_WINDOW_DEPTH);
	}

	while (m_bIsAlived)
	{
		//Start Counting Time
		FPSController::GetInstance()->StartCounter();

		glClear(GL_COLOR_BUFFER_BIT);

		//Check initialize data befor run game
		if (m_bIsPaused)
		{
			//TODO: Handle when game pause
			SceneManager::GetInstance()->Update(0.0f, true);
		}
		else
		{
			//TODO: Handle when game active
			SceneManager::GetInstance()->Update(0.0f, false);
		}
	
		//Update Client View 
		ViewController<ESWin32View>::GetInstance()->GetView()->Update();

		//End Counting Time And Compute Current FPS
		FPSController::GetInstance()->EndCounter();
		printf("FPS: %d\n", (__UINT32)FPSController::GetInstance()->GetRuntimeFPS());

		//Check Quit Message
		if (ViewController<ESWin32View>::GetInstance()->GetView()->IsQuit())
			this->Exit();
	};

	//Release all data before end game
	SceneManager::GetInstance()->Switch(0);
	SceneManager::GetInstance()->Update(0.0f, false);

	this->Destroy();
}

__VOID Game::Exit()
{
	m_bIsAlived = false;
}

__VOID Game::Pause()
{
	m_bIsPaused = true;
}

__VOID Game::Resume()
{
	m_bIsPaused = false;
}